﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Win32;

namespace YanMsg.APCode
{
    public class ConfigManger
    {
        public static string CurrentVersion ="";
     
        public static string PCName = "";
        //服务器端口
        public static string ServerIP ="";
        //服务器端口
        public static string ServerPort = "";

        //客户端默认接收地址
        public static string ServerRevDefaultPah = "";

        //自动更新地址
        public static string ClientUpdatePath = "";

        //默认背景图片
        public static string ClientUserBG = "";

        //默认皮肤
        public static string ClientUserSkin = "";

        //默认文件上传路径
        public static string ClientFileCenterUrl = "";

        //默认头像
        public static string ClientUserTX = "";

        //网站登录地址
        public static string ClientWebUrl = "";

        //默认登录账号
        public static string ClientUserName = "";

        //默认登录密码
        public static string ClientPassword = "";

        //默认是否自动登录
        public static string ClientisAutoRun = "";

        //默认是否开机启动
        public static string ClientisStartUp ="";


        public void initData()
        {

            PCName = GetPCName();

            ServerIP = GetConfig("ServerIP");
            ServerPort = GetConfig("ServerPort");


            ServerRevDefaultPah = GetConfig("ServerRevDefaultPah");


            ClientUpdatePath = GetConfig("ClientUpdatePath");


            ClientUserBG = GetConfig("ClientUserBG");


            ClientUserSkin = GetConfig("ClientUserSkin");


            ClientFileCenterUrl = GetConfig("ClientFileCenterUrl");


            ClientUserTX = GetConfig("ClientUserTX");


            ClientWebUrl = GetConfig("ClientWebUrl");


            ClientUserName = GetConfig("ClientUserName");


            ClientPassword = GetConfig("ClientPassword");


            ClientisAutoRun = GetConfig("ClientisAutoRun");


            ClientisStartUp = GetConfig("ClientisStartUp");
        }

        ///// <summary>
        ///// 服务器IP地址
        ///// </summary>
        ///// <returns>服务器IP地址</returns>
        //public static void CreateAccountInfo(string strAccount, string strPassword)
        //{
        //    try
        //    {
        //        string path = UserData.AppDirectory + "\\Config\\Config.xml";
        //        XElement root = XElement.Load(path);
        //        root.Element("strAccount").Value = strAccount;
        //        root.Element("strPassword").Value = strPassword;
        //        root.Save(path);
        //    }
        //    catch (Exception)
        //    {
        //        MsgManger.Show("无法读取配置文件，请联系管理员");
        //    }
        //}

        //public static void DelAccountInfo()
        //{
        //    try
        //    {
        //        string path = UserData.AppDirectory + "\\Config\\Config.xml";
        //        XElement root = XElement.Load(path);
        //        root.Element("strAccount").Value = "";
        //        root.Element("strPassword").Value = "";
        //        root.Save(path);
        //    }
        //    catch (Exception)
        //    {
        //        MsgManger.Show("无法读取配置文件，请联系管理员");
        //    }
        //}

        //public static ArrayList GetAccount()
        //{
        //    ArrayList AccountList = new ArrayList();

        //    try
        //    {
        //        string path = UserData.AppDirectory + "\\Config\\Config.xml";
        //        XElement root = XElement.Load(path);
        //        if (root.Element("strAccount") != null && root.Element("strAccount").Value.Trim()!="")
        //        {
        //            AccountList.Add(root.Element("strAccount").Value);
        //            AccountList.Add(root.Element("strPassword").Value);
        //        }
        //    }  
        //    catch (Exception)
        //    {
        //        MsgManger.Show("无法读取配置文件，请联系管理员");
        //    }
        //    return AccountList;

        //}




        //public static void CreateConfigInfo( string strConfigType,string strValue="Y")
        //{
        //    try
        //    {
        //        string path = UserData.AppDirectory + "\\Config\\Config.xml";
        //        XElement root = XElement.Load(path);
        //        if (root.Element(strConfigType) != null)
        //        {
        //            root.Element(strConfigType).Value = strValue;
        //            root.Save(path);
        //        }
        //    }
        //    catch (Exception)
        //    {
        //        MsgManger.Show("无法读取配置文件，请联系管理员");
        //    }
        //}

        //public static void DelConfigInfo(string strConfigType)
        //{
        //    try
        //    {
        //        string path = UserData.AppDirectory + "\\Config\\Config.xml";
        //        XElement root = XElement.Load(path);
        //        if (root.Element(strConfigType) != null)
        //        {
        //            root.Element(strConfigType).Value = "N";
        //            root.Save(path);
        //        }
        //    }
        //    catch (Exception)
        //    {
        //        MsgManger.Show("无法读取配置文件，请联系管理员");
        //    }
        //}

        //public static string GetConfigInfo(string strConfigType)
        //{
        //    try
        //    {
        //        string path = UserData.AppDirectory + "\\Config\\Config.xml";
        //        XElement root = XElement.Load(path);
        //        return root.Element(strConfigType).Value;
        //    }
        //    catch (Exception)
        //    {
        //        MsgManger.Show("无法读取配置文件，请联系管理员");
        //        return "";
        //    }
        //}


        //获取用户配置

        public string GetVersion()
        {
            try
            {
                return new Version(System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetEntryAssembly().Location).ProductVersion).ToString();
            }
            catch (Exception)
            {
                return "2.16";
            }
        }



        public  string GetConfig(string strKey)
        {
            string strReturn = "";
            YanChat_Config model = new YanChat_Config();
            model = new YanChat_ConfigB().GetEntity(d => d.keyowner == ConfigManger.PCName && d.key == strKey);
            if (model == null)
            {
                model = new YanChat_ConfigB().GetEntity(d => d.keyowner == "YanChat" && d.key == strKey);
            }
            if (model != null)
            {
                strReturn = model.value;
            }
            return strReturn;
        }

        public  string GetPCName()
        {
            try
            {
                return System.Net.Dns.GetHostName();
            }
            catch (Exception)
            {

                return "";
            }
        }
        //设置用户配置
        public  void SetConfig(string strKey, string strValue)
        {
            YanChat_Config model = new YanChat_ConfigB().GetEntity(d => d.keyowner == ConfigManger.PCName && d.key == strKey);
            if (model == null)
            {
                new YanChat_ConfigB().Insert(new YanChat_Config { key = strKey, value = strValue, keyowner = ConfigManger.PCName, crdate = DateTime.Now });
            }
            else
            {
                model.value = strValue;
                new YanChat_ConfigB().Update(model);
            }
        }


        //删除用户配置
        public  void DelConfig(string strKey)
        {
            new YanChat_ConfigB().Delete(d => d.keyowner == ConfigManger.PCName && d.key == strKey);
        }


        //设置开机自启动
        public  bool SetAutoRun(string keyName, string filePath)
        {
            try
            {
                RegistryKey runKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

                if (runKey == null)
                {
                    runKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
                }
                runKey.SetValue(keyName, filePath);

                runKey.Close();
            }

            catch
            {
                return false;
            }

            return true;
        }

        //去除开机自启动
        public  bool DeleteAutoRun(string keyName)
        {
            try
            {
                RegistryKey runKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

                runKey.DeleteValue(keyName, false);

                runKey.Close();
            }

            catch
            {
                return false;
            }

            return true;
        }

    }


    public class YanCommand
    {
        public static string UpdateGroup = "UpdateGroup";

    }


}